package com.kitty.game.jiehun;

import com.kitty.game.config.NPC;
import com.kitty.game.confirm.model.BuyWeddingConirm;
import com.kitty.game.confirm.model.LihunConfirm;
import com.kitty.game.confirm.model.RoleConfirm;
import com.kitty.game.friend.model.Friend;
import com.kitty.game.npc.message.RespNpcLeave;
import com.kitty.game.role.model.Role;
import com.kitty.game.role.service.RoleService;
import com.kitty.game.team.message.RespConfirm;
import com.kitty.game.team.message.RespMsg;
import com.kitty.game.team.model.Member;
import com.kitty.game.team.model.Team;
import com.kitty.game.team.service.TeamService;
import com.kitty.mina.message.Message;
import com.kitty.mina.message.MessagePusher;
import java.text.MessageFormat;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class JiehunService {
  public static volatile boolean WEDDING = false;
  
  public static volatile boolean YOUXING = false;
  
  @Autowired
  private RoleService roleService;
  
  @Autowired
  private TeamService teamService;





  
  public void init() {}
  
  public synchronized void npcButton(Role role, String msg, NPC npc) {
    if (StringUtils.isBlank(msg))
      return; 
    if ("tiqin_dispatch".equals(msg)) {
      Team team = this.teamService.getTeam(role.getRoleId());
      if (team != null) {
        List<Member> list = team.getList();
        int leaderId = team.getLeaderRoleId();
        int otherId = -1;
        Member leader = null;
        Member other = null;
        if (list.size() == 2) {
          for (Member member : list) {
            if (member.getRoleId() == leaderId) {
              leader = member;
              continue;
            } 
            other = member;
          } 
          Role leaderRole = this.roleService.getOnlinePlayer(leader.getUid());
          Role otherRole = this.roleService.getOnlinePlayer(other.getUid());
          if (leaderRole.getGender() == 2) {
            String msg2 = "请让新郎当队长";
            MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
            RespNpcLeave respNpcLeave1 = new RespNpcLeave();
            respNpcLeave1.setNpcId(npc.getId());
            MessagePusher.pushMessage(role, (Message)respNpcLeave1);
            return;
          } 
          Role roleMan = null;
          Role roleWoman = null;
          if (leaderRole.getGender() == 1) {
            roleMan = leaderRole;
            roleWoman = otherRole;
          } else {
            roleWoman = leaderRole;
            roleMan = otherRole;
          } 
          if (roleMan.getLoverId() == roleWoman.getRoleId() && roleWoman.getLoverId() == roleMan.getRoleId()) {
            RespConfirm respConfirm = new RespConfirm();
            String confirmMsg = MessageFormat.format("你确定要花费#R{0}积分#n来离婚吗？", new Object[] { Integer.valueOf(200) });
            respConfirm.setTips(confirmMsg);
            respConfirm.setPara_str("{}");
            role.setConfirm((RoleConfirm)new LihunConfirm());
            MessagePusher.pushMessage(leaderRole, (Message)respConfirm);
          } else {
            String msg2 = "非夫妻不能离婚";
            MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
            RespNpcLeave respNpcLeave1 = new RespNpcLeave();
            respNpcLeave1.setNpcId(npc.getId());
            MessagePusher.pushMessage(role, (Message)respNpcLeave1);
            return;
          } 
        } else {
          String msg2 = "组队人数为2方可离婚";
          MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
        } 
      } else {
        String msg2 = "组队人数为2方可离婚";
        MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
      } 
      RespNpcLeave respNpcLeave = new RespNpcLeave();
      respNpcLeave.setNpcId(npc.getId());
      MessagePusher.pushMessage(role, (Message)respNpcLeave);
    } else if ("buy_weddinglist".equals(msg)) {
      if (WEDDING) {
        String msg2 = "有婚礼正在举办中，请稍后再试";
        MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
        RespNpcLeave respNpcLeave = new RespNpcLeave();
        respNpcLeave.setNpcId(npc.getId());
        MessagePusher.pushMessage(role, (Message)respNpcLeave);
        return;
      } 
      Team team = this.teamService.getTeam(role.getRoleId());
      if (team != null) {
        List<Member> list = team.getList();
        int leaderId = team.getLeaderRoleId();
        int otherId = -1;
        Member leader = null;
        Member other = null;
        if (list.size() == 2) {
          for (Member member : list) {
            if (member.getRoleId() == leaderId) {
              leader = member;
              continue;
            } 
            other = member;
          } 
          Role leaderRole = this.roleService.getOnlinePlayer(leader.getUid());
          Role otherRole = this.roleService.getOnlinePlayer(other.getUid());
          if (leaderRole.getGender() == 2) {
            String msg2 = "请让新郎当队长";
            MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
            RespNpcLeave respNpcLeave = new RespNpcLeave();
            respNpcLeave.setNpcId(npc.getId());
            MessagePusher.pushMessage(role, (Message)respNpcLeave);
            return;
          } 
          Role roleMan = null;
          Role roleWoman = null;
          if (leaderRole.getGender() == 1) {
            roleMan = leaderRole;
            roleWoman = otherRole;
          } else {
            roleWoman = leaderRole;
            roleMan = otherRole;
          } 
          if (leaderRole.getMount() != 0 || otherRole.getMount() != 0) {
            String msg2 = "请先卸下所有成员的坐骑再举办婚礼";
            MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
            RespNpcLeave respNpcLeave = new RespNpcLeave();
            respNpcLeave.setNpcId(npc.getId());
            MessagePusher.pushMessage(role, (Message)respNpcLeave);
            return;
          } 
          if (leaderRole.getLoverId() > 0 || otherRole.getLoverId() > 0) {
            String msg2 = "队伍中存在已婚成员, 结婚失败";
            MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
            RespNpcLeave respNpcLeave = new RespNpcLeave();
            respNpcLeave.setNpcId(npc.getId());
            MessagePusher.pushMessage(role, (Message)respNpcLeave);
            return;
          } 
          Friend friend = leaderRole.getFriendBox().getFriendByGid(otherRole.getGid());
          if (friend == null) {
            String msg2 = "非好友关系, 无法结婚";
            MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
            RespNpcLeave respNpcLeave = new RespNpcLeave();
            respNpcLeave.setNpcId(npc.getId());
            MessagePusher.pushMessage(role, (Message)respNpcLeave);
            return;
          } 
          int friendness = friend.getFriendliness();
          if (friendness < 2000) {
            String msg2 = "友好度不足2000, 无法结婚";
            MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
            RespNpcLeave respNpcLeave = new RespNpcLeave();
            respNpcLeave.setNpcId(npc.getId());
            MessagePusher.pushMessage(role, (Message)respNpcLeave);
            return;
          } 
          RespConfirm respConfirm = new RespConfirm();
          String confirmMsg = MessageFormat.format("你确定要花费#R{0}积分#n来举办此次婚礼吗？", new Object[] { Integer.valueOf(2000) });
          respConfirm.setTips(confirmMsg);
          respConfirm.setPara_str("{}");
          leaderRole.setConfirm((RoleConfirm)new BuyWeddingConirm());
          MessagePusher.pushMessage(leaderRole, (Message)respConfirm);
        } else {
          String msg2 = "带上你的另一半举办婚礼";
          MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
          RespNpcLeave respNpcLeave = new RespNpcLeave();
          respNpcLeave.setNpcId(npc.getId());
          MessagePusher.pushMessage(role, (Message)respNpcLeave);
          return;
        } 
      } else {
        String msg2 = "带上你的另一半举办婚礼";
        MessagePusher.pushMessage(role, (Message)new RespMsg(msg2));
        RespNpcLeave respNpcLeave = new RespNpcLeave();
        respNpcLeave.setNpcId(npc.getId());
        MessagePusher.pushMessage(role, (Message)respNpcLeave);
        return;
      } 
    } else {
      RespNpcLeave respNpcLeave = new RespNpcLeave();
      respNpcLeave.setNpcId(npc.getId());
      MessagePusher.pushMessage(role, (Message)respNpcLeave);
    } 
  }
}
